<template>
  <div class="app-container">
    <div class="default-box">
      <div class="filter-container">
        <el-button class="filter-item" type="primary" icon="el-icon-plus" round @click="createDialog = true;isadd = true">
          新增
        </el-button>
      </div>
      <el-table
        :key="tableKey"
        v-loading="listLoading"
        :data="list"
        fit
        highlight-current-row
        style="width: 100%;"
      >
        <el-table-column label="名字" min-width="250" show-overflow-tooltip>
          <template>
            <span>消毒测温消毒测温消毒测温消毒测温消毒测温</span>
          </template>
        </el-table-column>
        <el-table-column label="描述" min-width="300" show-overflow-tooltip>
          <template>
            <span>为保障您和其他客人的安全，每日整车消毒且随车配备消毒及防护用品；司机、导服保证完成疫苗接种，上团前确保健康码绿码并体温正常；司机、导游每日主动体温监测并全程科学佩戴口罩。</span>
          </template>
        </el-table-column>
        <el-table-column label="排序" width="250">
          <template>
            <span>1</span>
          </template>
        </el-table-column>
        <el-table-column label="背景图" width="250">
          <template>
            <el-image
              style="width: 38px; height: 38px"
              :src="srcurl"
              :preview-src-list="srcList"
            />
          </template>
        </el-table-column>
        <el-table-column label="状态" width="250">
          <template>
            <span class="success-color">启用</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="250" class-name="small-padding fixed-width">
          <template>
            <el-button type="primary" round @click="createDialog = true;isadd = false">
              修改
            </el-button>
            <el-button type="danger" round @click="handleDelete">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
    <!-- 展示新增、编辑对话框 -->
    <el-dialog
      :title="isadd?'新增':'修改'"
      :visible.sync="createDialog"
      width="50%"
      custom-class="operate-dialog"
      top="30vh"
      center
    >
      <el-form :model="oprateform" label-width="80px">
        <el-form-item label="分类名字">
          <el-input v-model="oprateform.name" placeholder="请输入分类名字" />
        </el-form-item>
        <el-form-item label="分类简介">
          <el-input v-model="oprateform.name" placeholder="请输入分类简介" />
        </el-form-item>
        <el-form-item label="背景图片">
          <el-upload
            class="upload-demo"
            action="https://jsonplaceholder.typicode.com/posts/"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :file-list="fileList"
            list-type="picture"
          >
            <el-button type="primary">上传图片</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="oprateform.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="oprateform.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleReset">重置</el-button>
        <el-button type="primary" @click="handleOperate">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList } from '@/api/article'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'Adminrole',
  components: { Pagination },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        title: ''
      },
      srcurl: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
      srcList: [
        'https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg',
        'https://fuss10.elemecdn.com/1/8e/aeffeb4de74e2fde4bd74fc7b4486jpeg.jpeg'
      ],
      oprateform: { // 新增、编辑条件
        name: '',
        describe: '',
        sort: '',
        status: ''
      },
      isadd: true, // 是新增 否编辑
      createDialog: false, // 是否展示新增、编辑对话框
      fileList: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleOperate() {
      this.createDialog = false
    },
    // 重置
    handleReset() {
      this.oprateform = {
        name: '',
        describe: '',
        sort: ''
      }
    },
    // 删除
    handleDelete(row, index) {
      this.$confirm('确定要删除该记录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        center: true,
        customClass: 'text-center'
      }).then(() => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        // this.$message({
        //   type: 'success',
        //   message: '删除成功!'
        // })
      }).catch(() => {

      })
      this.list.splice(index, 1)
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePreview(file) {
      console.log(file)
    }
  }
}
</script>
<style scoped>
  [class^=el-icon-], [class*=" el-icon-"]{
    font-size: 16px;
  }
  .small-padding i{
    margin-right: 12px;
  }
  .small-padding i:last-child{
    margin-right: 0px;
  }
  ::v-deep .filter-container .el-input--medium .el-input__inner{
    width: 240px;
  }
</style>
